import http from "../service/http.js"
export const userModule = {
	state: {
		loading: false, //加载中
		isLoad: false, //已经加载
		userInfo: null, //用户信息
		// {
		// 	banji: ""
		// 	birthday: "2021-01-06"
		// 	city: "Guangzhou"
		// 	country: "China"
		// 	language: "zh_CN"
		// 	lastLoginIp: "58.62.93.180"
		// 	lastLoginTime: "2021-01-06 11:16:35"
		// 	mobile: "18202074132"
		// 	openid: "o80ap5ME7E9x_moXeGm6TA1fGWXA"
		// 	password: "e10adc3949ba59abbe56e057f20f883e"
		// 	pic: "https://thirdwx.qlogo.cn/mmopen/vi_32/LsvN12qdKFzkPXUZ28kSYr9ZHiabWhFypPOic2P8cD8YHyUCkGfLvC4DuSkZv8sCV7tpPsG6apDsXwfntKX7xFdw/132"
		// 	province: "Guangdong"
		// 	registerTime: "2021-01-06 08:13:08"
		// 	school: ""
		// 	sex: 1
		// 	source: 1
		// 	status: 1
		// 	unionid: null
		// 	userId: 3
		// 	userName: "18202074132"
		// 	userNick: "东俊"
		// 	wechat: "18202074132"
		// }
		userEditFrom: {
			// 用户信息编辑模块
			mobile: "", //手机
			userNick: "", //昵称
			school: "", //学校
			grade: "" //班级
		}

	},
	mutations: {
		setUserInfo(state, userInfo) {
			state.isLoad = true;
			state.userInfo = userInfo;
		},
		setUserEditFrom(state, userEditFrom) {
			state.userEditFrom = userEditFrom
		},
		setLoading(state, loading) {
			state.loading = loading
		},
		clearUserInfo(state, loading){
			state.userInfo=null
		}
	},
	getters: {

	},
	actions: {
		getUserInfo: async function({
			commit,
			state
		}) {
			return await new Promise((resolve, reject) => {
				commit("setLoading", true)
				http.get('/user/info').then(res => {
					commit("setLoading", false)
					console.log("获取用户信息成功", res)
					let data = res.data.data
					commit("setUserInfo", data)
					commit("setUserEditFrom", {
						mobile: data.mobile, //手机
						userNick: data.userNick, //昵称
						school: data.school, //学校
						grade: data.banji //班级
					})
					resolve(data)

				}).catch(err => {
					console.log("获取用户信息失败", err)
					reject(err)
				})
			})
		},
		editUserInfo: async function({
			commit,
			state
			// {
			// mobile: "", //手机
			// userNick: "", //昵称
			// school:"", //学校
			// banji:"" //班级	
			// }
		}, params) {
			// console.log("params", params)
			return await new Promise((resolve, reject) => {
				commit("setLoading", true)
				http.post('/user/update', {
					mobile: params.mobile, //手机
					userNick: params.userNick, //昵称
					school: params.school, //学校
					banji: params.grade //班级
				}).then(res => {
					commit("setLoading", false)
					console.log("编辑用户信息成功", res)
					let data = res.data.data
					commit("setUserInfo", data)
					commit("setUserEditFrom",
						params
						// {
						// 	mobile: data.mobile, //手机
						// 	userNick: data.userNick, //昵称
						// 	school: data.school, //学校
						// 	grade: data.banji //班级
						// },
					)
					resolve(data)

				}).catch(err => {
					console.log("编辑用户信息失败", err)
					reject(err)
				})
			})
		}

	}
}
